package com.cskaoyan.rocketmq.basic;

import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.io.UnsupportedEncodingException;

/**
 * 创建日期: 2023/01/12 15:46
 *
 * @author ciggar
 */
public class BasicProducer {

    // 消息生产者
    // 向RocketMQ发送消息

    public static void main(String[] args) throws MQClientException, UnsupportedEncodingException, RemotingException, InterruptedException, MQBrokerException {

        // 1. 构建消息生产者
        DefaultMQProducer producer = new DefaultMQProducer("basic_producer_group");

        // 2. 向注册中心注册
        producer.setNamesrvAddr("localhost:9876");

        // 3. 启动
        producer.start();

        String content = "hello rocketMQ , 46th";

        // 4. 构建一个消息
        Message message = new Message("test-46",content.getBytes("utf-8"));

        // 5. 发送消息
        SendResult sendResult = producer.send(message);

        long startTime = System.currentTimeMillis();
        System.out.println("发送消息的时间：" + startTime);

        // 获取消息的发送状态
        SendStatus sendStatus = sendResult.getSendStatus();

        System.out.println("sendStatus:" + sendStatus);
        System.out.println("sendResult:" + sendResult);


    }
}
